package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/storage")
public class DtsAdminStorageController {

    @Autowired
    DtsStorageService dtsStorageService;

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsStorage storage){
        DtsStorage dtsStorage=dtsStorageService.delete(storage);
        return ResponseUtil.ok(dtsStorage);
    }


    @RequestMapping("/update")
    public Object update(@RequestBody DtsStorage storage){
        DtsStorage dtsStorage=dtsStorageService.update(storage);
        return ResponseUtil.ok(dtsStorage);
    }

    @RequestMapping("/create")
    public Object creat(MultipartFile file) throws Exception {
        byte[] bytes = file.getBytes();
        String filename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        String newName=uuid+filename;
        long size = file.getSize();
        String type = file.getContentType();
        DtsStorage storage=dtsStorageService.add(bytes,newName,filename,(int)size,type);

        return ResponseUtil.ok(storage);
    }






    @RequestMapping("/list")
    public Object list(@RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(name = "limit",required = false,defaultValue = "10")Integer limit,
                       String sort,String order,String key,String name){

        IPage<DtsStorage> iPage=dtsStorageService.findByPage(page,limit,sort,order,key,name);
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);

    }
}
